package com.s69.appback.controller;

import com.s69.appback.entity.AppInfo;
import com.s69.appback.entity.AppVersion;
import com.s69.appback.service.AppInfoService;
import com.s69.appback.service.AppVersionService;
import com.s69.appback.vo.ResultVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class VersionController {
    @Autowired
    AppVersionService versionService;
    @Autowired
    AppInfoService appInfoService;
    @PostMapping("/versionList")
    public ResultVo versionList(Long appid){
        List<AppVersion> versionList=versionService.getList(appid);
        return ResultVo.success("",versionList);
    }

    @PostMapping("/saveVersion")
    public ResultVo saveVersion(AppVersion appVersion, MultipartFile apkFile, HttpServletRequest request){
        if (apkFile!=null && !apkFile.isEmpty()){
            String realPath = request.getServletContext().getRealPath("/upload/appinfo/apk");
            File file=new File(realPath);
            if (!file.exists()){
                file.mkdirs();
            }
            String originalFilename = apkFile.getOriginalFilename();
            String suffixName = FilenameUtils.getExtension(originalFilename);
            if (apkFile.getSize()>1024*500*1024){
                throw new RuntimeException("文件大小超过500M");
            } else if (suffixName.equalsIgnoreCase("apk")) {
                String newFilename = UUID.randomUUID().toString().replace("-", "") + "." + suffixName;
                File saveFile=new File(realPath+"/"+newFilename);
                try {
                    apkFile.transferTo(saveFile);
                    appVersion.setApkfilename(newFilename);
                    appVersion.setDownloadlink(("/upload/appinfo/apk/"+newFilename));
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultVo.error("文件上传异常！");
                }
            }else {
                return ResultVo.error("文件格式错误！");
            }
        }
        //数据库操作
        String token = request.getHeader("token");
        String[] split = token.split("-");
            //新增
            appVersion.setPublishstatus(3L);
            appVersion.setCreatedby(Long.valueOf(split[2]));
            appVersion.setCreationdate(new Date());
            appVersion.setModifyby(Long.valueOf(split[2]));
            appVersion.setModifydate(new Date());
            AppInfo appInfo=new AppInfo();
            appInfo.setId(appVersion.getAppid());
            appInfo.setStatus(1L);
            versionService.addVersions(appVersion);
            appInfo.setVersionid(appVersion.getId());
            appInfoService.update(appInfo);
        return ResultVo.success("",null);
    }
}
